package com.translate.web.service.impl;

import com.common.util.TheMagicSkillOfBeingInvisible;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.AdminRoleRelationMapper;
import com.translate.entity.AdminRoleRelation;
import com.translate.web.service.IAdminRoleRelationService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.IAdminRoleRelationAuthorityVO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class AdminRoleRelationServiceImpl extends BaseServiceImpl<AdminRoleRelation>
		implements IAdminRoleRelationService {

	private RedisTemplate<String, List<AdminRoleRelation>> redisTemplate;

	public AdminRoleRelationServiceImpl(AdminRoleRelationMapper mapper) {
		super(mapper);
	}

	protected Class<AdminRoleRelation> getEntityClass() {
		return AdminRoleRelation.class;
	}

	@Override
	public List<AdminRoleRelation> getAdminRoleRelations(int userId) {

		return roleMenuRelationToCache(userId);

	}

	/**
	 * 角色菜单缓存
	 * 
	 * @param id
	 * @return
	 */
	public List<AdminRoleRelation> roleMenuRelationToCache(int userId) {

		String key = RedisConstant.getAdminRoleRelations(userId);

		ValueOperations<String, List<AdminRoleRelation>> valueOperations = this.redisTemplate.opsForValue();

		List<AdminRoleRelation> order = valueOperations.get(key);

		if (null != order) {
			return order;
		}

		order = this.getDataByIds(userId);

		if (null == order) {
			order = new ArrayList<AdminRoleRelation>(0);

			valueOperations.set(key, order, 3, TimeUnit.SECONDS);

			return order;
		}

		valueOperations.set(key, order, 12, TimeUnit.SECONDS);

		return order;

	}

	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<AdminRoleRelation>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public int addHighSeas(List<Long> userId) {

		return this.batchInsert(this.getAdminRoleRelations(userId, 8));
	}

	protected List<AdminRoleRelation> getAdminRoleRelations(List<Long> longs, int privilege) {

		List<AdminRoleRelation> adminRoleRelations = new ArrayList<>();

		AdminRoleRelation adminRoleRelation;

		for (Long id : longs) {
			adminRoleRelation = new AdminRoleRelation();

			adminRoleRelation.setAdminId((int) TheMagicSkillOfBeingInvisible.GetDecodeUserID(id));

			adminRoleRelation.setRoleId(privilege);

			adminRoleRelations.add(adminRoleRelation);
		}

		return adminRoleRelations;

	}

	@Override
	public int addProduct(List<Long> userId) {
		return this.batchInsert(this.getAdminRoleRelations(userId, 12));

	}

	@Override
	public int addDepartment(List<Long> userId) {
		return this.batchInsert(this.getAdminRoleRelations(userId, 9));

	}

	@Override
	public byte topManager(int userId) {

		List<AdminRoleRelation> adminRoleRelations = this.roleMenuRelationToCache(userId);

		byte flag = 0;

		for (AdminRoleRelation adminRoleRelation : adminRoleRelations) {

			if (5 == adminRoleRelation.getRoleId()) {
				flag = 1;
				break;
			}
		}

		return flag;
	}

	@Override
	public <VO extends IAdminRoleRelationAuthorityVO> List<IAdminRoleRelationAuthorityVO> getAdminRoleRelationAuthorityVOs(
			Class<VO> target, int roleId) {

		// 8 公海角色编号
		List<AdminRoleRelation> adminRoleRelations = this.getRoleMenuRelationByRoleIdToCache(roleId);

		List<IAdminRoleRelationAuthorityVO> adminRoleRelationAuthorityVOs = new ArrayList<>(32);

		IAdminRoleRelationAuthorityVO adminRoleRelationAuthorityVO;

		try {

			for (AdminRoleRelation adminRoleRelation : adminRoleRelations) {

				adminRoleRelationAuthorityVO = target.newInstance();
				
				adminRoleRelationAuthorityVO.setAdminId(adminRoleRelation.getAdminId());
				
				adminRoleRelationAuthorityVO.setId(adminRoleRelation.getId());
				
				adminRoleRelationAuthorityVOs.add(adminRoleRelationAuthorityVO);

			}
		} catch (Exception e) {

			throw new Error(e);
		}

		return adminRoleRelationAuthorityVOs;

	}

	/**
	 * 角色菜单缓存
	 * 
	 * @param id
	 * @return
	 */
	public List<AdminRoleRelation> getRoleMenuRelationByRoleIdToCache(int roleId) {

		String key = RedisConstant.getHignSea();

		ValueOperations<String, List<AdminRoleRelation>> valueOperations = this.redisTemplate.opsForValue();

		List<AdminRoleRelation> order = valueOperations.get(key);

		if (null != order) {
			return order;
		}

		AdminRoleRelationMapper adminRoleRelationMapper = this.getBaseMapper();

		order = adminRoleRelationMapper.getHighSeas(roleId);

		if (null == order) {
			order = new ArrayList<AdminRoleRelation>(0);

			valueOperations.set(key, order, 3, TimeUnit.SECONDS);

			return order;
		}

		valueOperations.set(key, order, 12, TimeUnit.SECONDS);

		return order;

	}

}